package com.aid.winter.service.impl;

import com.aid.winter.common.constant.RoleConstants;
import com.aid.winter.common.exception.BusinessException;
import com.aid.winter.entity.UserWhitelist;
import com.aid.winter.entity.SysUser;
import com.aid.winter.mapper.UserWhitelistMapper;
import com.aid.winter.mapper.SysUserMapper;
import com.aid.winter.service.UserWhitelistService;
import com.aid.winter.dto.response.UserVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserWhitelistServiceImpl implements UserWhitelistService {

    private final UserWhitelistMapper whitelistMapper;
    private final SysUserMapper userMapper;

    @Override
    public boolean isUserInWhitelist(Long userId) {
        // 获取用户信息
        SysUser user = userMapper.selectById(userId);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }
        
        // 如果不是学校用户，则不需要检查白名单
        if (user.getRoleType() != RoleConstants.ROLE_SCHOOL) {
            return true;
        }
        
        // 检查是否在白名单中
        return whitelistMapper.selectByUserId(userId) != null;
    }

    @Override
    public List<UserVO> getWhitelistUsers() {
        List<UserWhitelist> whitelists = whitelistMapper.selectList();
        List<UserVO> userVOs = new ArrayList<>();
        
        for (UserWhitelist whitelist : whitelists) {
            SysUser user = userMapper.selectById(whitelist.getUserId());
            if (user != null) {
                UserVO vo = new UserVO();
                BeanUtils.copyProperties(user, vo);
                userVOs.add(vo);
            }
        }
        
        return userVOs;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addToWhitelist(Long userId, Long adminId) {
        // 检查用户是否存在且是学校用户
        SysUser user = userMapper.selectById(userId);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }
        
        if (user.getRoleType() != RoleConstants.ROLE_SCHOOL) {
            throw new BusinessException("只能将学校用户添加到白名单中");
        }
        
        // 检查是否已在白名单中
        if (whitelistMapper.selectByUserId(userId) != null) {
            throw new BusinessException("该用户已在白名单中");
        }
        
        // 添加到白名单
        UserWhitelist whitelist = new UserWhitelist();
        whitelist.setUserId(userId);
        whitelist.setCreatorId(adminId);
        whitelist.setCreateTime(LocalDateTime.now());
        whitelistMapper.insert(whitelist);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeFromWhitelist(Long userId) {
        // 检查用户是否在白名单中
        if (whitelistMapper.selectByUserId(userId) == null) {
            throw new BusinessException("该用户不在白名单中");
        }
        
        whitelistMapper.deleteByUserId(userId);
    }
} 